﻿function FactoryXMLHttpRequest() {
    if(window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    else if(window.ActiveXObject) {
        var msxmls = new Array(
        'Msxml2.XMLHTTP.5.0',
        'Msxml2.XMLHTTP.4.0',
        'Msxml2.XMLHTTP.3.0',
        'Msxml2.XMLHTTP',
        'Microsoft.XMLHTTP');
        for (var i = 0; i < msxmls.length; i++) {
            try {
                return new ActiveXObject(msxmls[i]);
            } 
            catch (e) 
            {
            }
        }
    }
    throw new Error("Could not instantiate XMLHttpRequest");
}
function get_xml_file (url) {
    var httpreq = new XMLHttpRequest(); //FactoryXMLHttpRequest();

    //Precondition: must have a URL
    if (url == "") return;

    httpreq.open("GET", url, true);

    httpreq.onreadystatechange = function (  ) {
        if (httpreq.readyState == 4) {
            var content = document.getElementById("DivMainContent");
            content.innerHTML = "Parsing XML...<br>";

            last_xml_response = httpreq.responseXML;
            format_rss_data ("DivMainContent", last_xml_response);
        }
    }
    var content = document.getElementById("DivMainContent");
    content.innerHTML = "<img src='loading.gif'>"		//"Retrieving XML...<br>";
    httpreq.send (null);
}
function get_rss_feed (  ) {

    //Get selected RSS feed
    var lbFeeds = document.getElementById("lbFeeds");
    if (lbFeeds.value != "") {
        get_xml_file (lbFeeds.value);
    }     
}
function format_rss_data (divname, response) {
    var html = "";
    var doc = response.documentElement;
    var items = doc.getElementsByTagName('item');

    for (var i=0; i < items.length; i++) {

        var title = items[i].getElementsByTagName('title')[0];
        var link = items[i].getElementsByTagName('link')[0];

        html += "<b><a href='" 
             + link.firstChild.data 
             + "' target=_blank >" 
             + title.firstChild.data 
             + "</a></b><br>";

        var cbDetails = document.getElementById("cbDetails");
        if (cbDetails.checked) {
            var desc = items[i].getElementsByTagName('description')[0];
            html += "<font size='-1'>" 
                    + desc.firstChild.data 
                    + "</font><p>";
        } 
    }
    var target_div = document.getElementById(divname);
    target_div.innerHTML = html;
}
